<template>
  <div class="app-container">
    <div style="width: 60px;height: 60px;margin-left: 0px;margin-top: 0px">
      <el-button icon="el-icon-s-data" class="custom-button">总数<br><br>{{total}}</el-button>
    </div>
    <div style="width: 1100px;height: 60px;margin-left: 60px;margin-top: -60px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <!--      <el-form-item label="所属小区的ID" prop="plotId">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.plotId"-->
        <!--          placeholder="小区的ID"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <el-form-item label="楼栋" prop="buildingsId">
          <el-input
            v-model="queryParams.buildingsId"
            placeholder="楼栋"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!--      <el-form-item label="单元的ID" prop="unitsId">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.unitsId"-->
        <!--          placeholder="请输入所属单元的ID"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--      <el-form-item label="楼层的ID" prop="floorsId">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.floorsId"-->
        <!--          placeholder="所属楼层的ID"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <el-form-item label="房间号" prop="roomNumber">
          <el-input
            v-model="queryParams.roomNumber"
            placeholder="请输入房间号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="房屋类型" prop="roomTypeId">
          <el-select v-model="queryParams.roomTypeId" placeholder="全部" clearable>
            <el-option
              v-for="dict in dict.type.estate_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <hr>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:estate:add']"
        >新增</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['system:estate:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['system:estate:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:estate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="estateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" width="50">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="小区名称" align="center" prop="plotName" />
      <el-table-column label="楼栋" align="center" prop="buildingsId" />
      <el-table-column label="单元" align="center" prop="unitsId" />
      <el-table-column label="楼层" align="center" prop="floorsId" />
      <el-table-column label="房间号" align="center" prop="roomNumber" />
      <el-table-column label="房屋类型" align="center" prop="roomTypeName" />
      <el-table-column label="档案" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="personFile(scope.row)"
          >一屋一档</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:estate:edit']"
          >修改</el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['system:estate:remove']"-->
          <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 添加或修改实有房屋对话框  :rules="rules"-->
    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body top="300px" left="50px">
      <el-form :inline="true" ref="form" :model="form" label-width="80px">
        <div style="width: 800px">
          <el-form-item label="小区名称" prop="plotId">
            <el-select v-model="form.plotId" placeholder="请选择小区" clearable>
              <el-option v-for="i in plotList" :key="i.id" :label="i.plotName" :value="i.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="楼栋" prop="buildingsId">
            <el-input v-model="form.buildingsId" style="width: 150px" placeholder="请输入楼栋"><template slot="append">栋</template></el-input>
          </el-form-item>
        </div>
        <el-form-item label="单元" prop="unitsId">
          <el-input v-model="form.unitsId" placeholder="请输入单元的编号" />
        </el-form-item>
        <el-form-item label="起始楼层" prop="buildingsId">
          <el-input v-model="form.startFloor" style="width: 150px" placeholder="请输入"><template slot="append">层</template></el-input>
        </el-form-item>
        <el-form-item label="结束楼层" prop="buildingsId">
          <el-input v-model="form.endFloor" style="width: 150px" placeholder="请输入"><template slot="append">层</template></el-input>
        </el-form-item>
        <el-form-item label="起始户数" prop="buildingsId">
          <el-input v-model="form.initialHousehold" style="width: 150px" placeholder="请输入"><template slot="append">户</template></el-input>
        </el-form-item>
        <el-form-item label="每层户数" prop="buildingsId">
          <el-input v-model="form.numberFloors" style="width: 150px" placeholder="请输入"><template slot="append">户</template></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改类型" :visible.sync="dialogFormVisible">
      <el-form :inline="true" ref="form" :model="form" label-width="80px">
        <el-form-item label="房屋类型">
          <el-select v-model="form.roomTypeId" placeholder="全部" clearable>
            <el-option
              v-for="dict in dict.type.estate_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelEstate">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {addEstate, delEstate, listEstate, updateEstate} from "@/api/system/smart/estate";
import {listPlots} from "@/api/system/smart/plot";

export default {
  name: "Estate",
  dicts: ['estate_type'],
  data() {
    return {
      dialogFormVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实有房屋
      // 表格数据
      estateList: [],
      plotList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plotId: null,
        buildingsId: null,
        unitsId: null,
        floorsId: null,
        roomNumber: null,
        roomTypeId: null
      },
      // 表单参数
      form: {
        plotId: '',
        buildingsId: '',
        unitsId: '',
        startFloor: '',
        endFloor: '',
        initialHousehold: '',
        numberFloors: ''
      },

      // 表单校验
      // rules: {
      //   plotId: [
      //     { required: true, message: "所属小区不能为空", trigger: "blur" }
      //   ],
      //   buildingsId: [
      //     { required: true, message: "所属楼栋不能为空", trigger: "blur" }
      //   ],
      //   unitsId: [
      //     { required: true, message: "所属单元不能为空", trigger: "blur" }
      //   ],
      //   floorsId: [
      //     { required: true, message: "所属楼层不能为空", trigger: "blur" }
      //   ],
      //   roomNumber: [
      //     { required: true, message: "房间号不能为空", trigger: "blur" }
      //   ],
      // }
      user: {},
      userId: ''
    };
  },
  created() {
    this.user = JSON.parse(sessionStorage.getItem('user'))
    this.getList();
    this.getPlotList();
  },
  methods: {
    getPlotList() {
      this.userId = this.user.userId
      listPlots(this.userId).then(res=>{
        this.plotList = res.data;
      })
    },
    personFile(row) {
      this.$router.push({path: '/community/archivess/estate', query: {estate: row}});
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      console.log(`每页 ${val} 条`);
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.pageNum = val
      this.getList();
    },

    /** 查询实有房屋列表 */
    getList() {
      this.loading = true;
      listEstate(this.queryParams).then(response => {
        console.log(response)
        this.estateList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelEstate() {
      this.dialogFormVisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {};
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实有房屋";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(JSON.stringify(row))
      this.dialogFormVisible = true
      this.form = row
      this.form.roomTypeId = row.roomTypeId + ""
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEstate(this.form.roomTypeId, this.form.id).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.dialogFormVisible = false;
              this.getList();
            });
          } else {
            addEstate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除实有房屋编号为"' + ids + '"的数据项？').then(function () {
    //     return delEstate(ids);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {
    //   });
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/estate/export', {
        ...this.queryParams
      }, `estate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.custom-button {
  border: none; /* 移除边框 */
  background-color: transparent; /* 移除背景色 */
  color: inherit; /* 继承父元素颜色 */
  padding: 0; /* 移除内边距 */
}

.custom-button .el-icon-s-data {
  display: none; /* 隐藏图标 */
}

.custom-button span {
  font-size: 14px; /* 调整字体大小 */
}
</style>
